package visualInterface;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

public class AddSeasonFrame extends JFrame {
	private JLabel edition = new JLabel("Editie:");
	private JLabel season = new JLabel("Nume sezon: ");
	private JLabel rounds = new JLabel("Numar etape: ");
	private JLabel teams = new JLabel("Numar echipe: ");
	private JLabel teamsName = new JLabel("Nume echipa: ");

	private JTextField seasonEdition = new JTextField();
	private JTextField seasonName = new JTextField();
	private JTextField roundsNumber = new JTextField();
	private JTextField teamsNumber = new JTextField();

	private JComboBox teamName = new JComboBox();

	private JButton saveSeason = new JButton("Salveaza sezon");
	private JButton saveTeam = new JButton("Salveaza");
	private JButton cancelTeam = new JButton("Anuleaza");
	private JButton deleteTeam = new JButton("Sterge");

	BackgroungImages mainPanel;

	public AddSeasonFrame() {
		setFrame();
	}

	public void addComponent() {
		mainPanel = new BackgroungImages("details.jpg", this);
		GridLayout mainPanelLayout = new GridLayout(1, 3);
		mainPanel.setLayout(mainPanelLayout);

		JPanel leftPanel = new JPanel();
		leftPanel.setOpaque(false);
		JPanel centerPanel = new JPanel();
		GridLayout centerPanelLayout = new GridLayout(3, 1);
		centerPanel.setLayout(centerPanelLayout);
		centerPanel.setOpaque(false);

		JPanel centerUpperPanel = new JPanel();
		centerUpperPanel.setOpaque(false);
		centerPanel.add(centerUpperPanel);
		JPanel rightPanel = new JPanel();
		rightPanel.setOpaque(false);

		JPanel panel = new JPanel();
		SpringLayout layout1 = new SpringLayout();
		panel.setLayout(layout1);
		panel.setOpaque(false);

		panel.add(edition);
		panel.add(seasonEdition);
		
		layout1.putConstraint(SpringLayout.WEST, edition, 10, SpringLayout.WEST,
				panel);
		layout1.putConstraint(SpringLayout.NORTH, edition, 10,
				SpringLayout.NORTH, panel);
		layout1.putConstraint(SpringLayout.WEST, seasonEdition, 5,
				SpringLayout.EAST, teams);
		layout1.putConstraint(SpringLayout.NORTH, seasonEdition, 10,
				SpringLayout.NORTH, panel);
		
		panel.add(season);
		panel.add(seasonName);
		
		layout1.putConstraint(SpringLayout.WEST, season, 10, SpringLayout.WEST,
				panel);
		layout1.putConstraint(SpringLayout.NORTH, season, 10,
				SpringLayout.SOUTH, edition);
		layout1.putConstraint(SpringLayout.WEST, seasonName, 5,
				SpringLayout.EAST, teams);
		layout1.putConstraint(SpringLayout.NORTH, seasonName, 10,
				SpringLayout.SOUTH, edition);

		panel.add(teams);
		panel.add(teamsNumber);

		layout1.putConstraint(SpringLayout.WEST, teams, 10, SpringLayout.WEST,
				panel);
		layout1.putConstraint(SpringLayout.NORTH, teams, 10,
				SpringLayout.SOUTH, season);
		layout1.putConstraint(SpringLayout.WEST, teamsNumber, 5,
				SpringLayout.EAST, teams);
		layout1.putConstraint(SpringLayout.NORTH, teamsNumber, 10,
				SpringLayout.SOUTH, season);

		panel.add(rounds);
		panel.add(roundsNumber);

		layout1.putConstraint(SpringLayout.WEST, rounds, 10, SpringLayout.WEST,
				panel);
		layout1.putConstraint(SpringLayout.NORTH, rounds, 10,
				SpringLayout.SOUTH, teams);
		layout1.putConstraint(SpringLayout.WEST, roundsNumber, 5,
				SpringLayout.EAST, teams);
		layout1.putConstraint(SpringLayout.NORTH, roundsNumber, 10,
				SpringLayout.SOUTH, teams);
		
		panel.add(saveSeason);
		layout1.putConstraint(SpringLayout.WEST, saveSeason, 70, SpringLayout.WEST,
				panel);
		layout1.putConstraint(SpringLayout.NORTH, saveSeason,30,
				SpringLayout.SOUTH, rounds);

		panel.add(teamName);
		panel.add(teamsName);
		panel.add(saveTeam);
		panel.add(cancelTeam);
		panel.add(deleteTeam);

		layout1.putConstraint(SpringLayout.WEST, teamsName, 10,
				SpringLayout.WEST, panel);
		layout1.putConstraint(SpringLayout.NORTH, teamsName, 30,
				SpringLayout.SOUTH, saveSeason);

		layout1.putConstraint(SpringLayout.WEST, teamName, 10,
				SpringLayout.EAST, teams);
		layout1.putConstraint(SpringLayout.NORTH, teamName, 30,
				SpringLayout.SOUTH, saveSeason);

		layout1.putConstraint(SpringLayout.WEST, saveTeam, 10,
				SpringLayout.WEST, panel);
		layout1.putConstraint(SpringLayout.NORTH, saveTeam, 30,
				SpringLayout.SOUTH, teamsName);

		layout1.putConstraint(SpringLayout.WEST, cancelTeam, 10,
				SpringLayout.EAST, teams);
		layout1.putConstraint(SpringLayout.NORTH, cancelTeam, 30,
				SpringLayout.SOUTH, teamsName);

		layout1.putConstraint(SpringLayout.WEST, deleteTeam, 10,
				SpringLayout.EAST, cancelTeam);
		layout1.putConstraint(SpringLayout.NORTH, deleteTeam, 30,
				SpringLayout.SOUTH, teamsName);

		centerPanel.add(panel);
		mainPanel.add(leftPanel);
		mainPanel.add(centerPanel);
		mainPanel.add(rightPanel);
		getContentPane().add(mainPanel);
	}

	public void hideFrame() {
		setVisible(false);
	}

	private void setComponent() {
		seasonEdition.setPreferredSize(new Dimension(140,20));
		seasonEdition.setBorder(BorderFactory.createEtchedBorder(0));
		seasonEdition.setBackground(Color.WHITE);
		
		seasonName.setPreferredSize(new Dimension(140, 20));
		seasonName.setBorder(BorderFactory.createEtchedBorder(0));
		seasonName.setBackground(Color.WHITE);

		roundsNumber.setPreferredSize(new Dimension(50, 20));
		roundsNumber.setBorder(BorderFactory.createEtchedBorder(0));
		roundsNumber.setBackground(Color.WHITE);

		teamsNumber.setPreferredSize(new Dimension(50, 20));
		teamsNumber.setBorder(BorderFactory.createEtchedBorder(0));
		teamsNumber.setBackground(Color.WHITE);

		teamName.setPreferredSize(new Dimension(140, 20));
		teamName.setBorder(BorderFactory.createEtchedBorder(0));
		teamName.setBackground(Color.WHITE);
		teamName.setEnabled(false);
		saveTeam.setEnabled(false);
		deleteTeam.setEnabled(false);
		cancelTeam.setEnabled(false);
	}

	private void setFrame() {
		setComponent();
		addComponent();
		setFramePosition();
	}

	public void setFramePosition() {
		Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameDimension = new Dimension(850, 850);
		setSize(frameDimension);
		Point leftCorner = new Point();
		leftCorner.x = (screenDimension.width - frameDimension.width) / 2;
		leftCorner.y = (screenDimension.height - frameDimension.height) / 2;
		setLocation(leftCorner);
		setMinimumSize(frameDimension);
	}

	public void showFrame() {
		setVisible(true);
	}
}
